/**
 *  File: BindCardService.java
 *  Date         Author      Changes
 *  2016-10-12    wangtq       Create
 *
 */
package com.kunpu.mami.channel.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.kunpu.mami.channel.common.ResConstants;
import com.kunpu.mami.channel.exception.MamiiException;

/**
 * 宝付绑卡服务
 * @author wangtq
 * @date 2016-10-12
 * @version 0.1
 *
 */
@Service
public class BindCardService extends BaseSendRepayService{
	private final Logger       logger = LoggerFactory.getLogger(this.getClass());
	
		/**
	    * 
	    * @param trans_id   交易流水号
	    * @param bind_id    银行返回bindId
	    * @param txn_amt    交易金额
	    * @param sms_code   短信码
	    * @param additional_info 
	    * @param req_reserved
	    * @return
	    */
	public void sendBind(String accNo,String id_card,String id_holder,String mobile,String pay_code,String sms_code,String trans_id){
		logger.info("===================================== 渠道service sendBind 绑卡 start ,"
				+ "accNo{},id_card{},id_holder{},mobile{},pay_code{},sms_code{},trans_id{}:",
				   accNo,id_card,id_holder,mobile,pay_code,sms_code,trans_id);
		try{
			String dataContent = buildSendBindParam(accNo, id_card, id_holder,mobile,pay_code,sms_code,trans_id);
			String txn_sub_type = "01";// 交易子类
			send(dataContent, "sendBind",txn_sub_type);
		}catch(MamiiException me){
			logger.error("==================== sendBind 绑卡异常，异常信息为：",me.getMessage(),me);
			throw new MamiiException(me.getErrorCode() , me.getMessage());
		}catch(Exception e){
			logger.error("==================== sendBind 绑卡时异常，异常信息为：",e.getMessage(),e);
			throw new MamiiException(ResConstants.PAY_ANALYSIS_EXISTENT.getCode() , "绑卡时异常");
		}
		logger.info("====================================== 渠道service sendBind 绑卡 end ===============================");
	}
	
	
	/**
	 * 获取绑卡报文
	 * 
	 * @param trans_id  唯一订单号，8-20
	 * @param acc_no    绑定的卡号
	 * @param mobile    银行卡绑定手机号
	 * @param id_card    身份证号
	 * @param id_holder  卡号持有人
	 * @param acc_pwd    卡号密码 交易金额 单位：例：1元则提交为100
	 * @param valid_date  卡有效期
	 * @param valid_no    卡安全码
	 * @param pay_code    银行编码
	 * @param sms_code   【短信验证码】
	 * @param additional_info
	 * @param req_reserved
	 * @return
	 */
	public String buildSendBindParam(String accNo,String id_card,String id_holder,String mobile,String pay_code,String sms_code,String trans_id) {
		String trade_date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String txn_sub_type = "01";// 交易子类
		StringBuilder sb = new StringBuilder();
		String additionalInfo = "附加字段";
		String reqReserved = "请求方保留域";
		sb.append("{");
		sb.append("\"txn_sub_type\":\"" + txn_sub_type + "\"");
		sb.append(",\"biz_type\":\"0000\"");// 接入类型默认0000
		sb.append(",\"terminal_id\":\"" + terminal_id + "\"");
		sb.append(",\"member_id\":\"" + member_id + "\"");
		sb.append(",\"trans_serial_no\":\"" + member_id + "" + trade_date + "\"");
		sb.append(",\"trans_id\":\"" + trans_id + "\"");
		sb.append(",\"acc_no\":\"" + accNo + "\"");
		sb.append(",\"id_card_type\":\""+id_card_type+"\"");// 默认01为身份证号
		sb.append(",\"id_card\":\"" + id_card + "\"");
		sb.append(",\"id_holder\":\"" + id_holder + "\"");// 持卡人姓名
		sb.append(",\"mobile\":\"" + mobile + "\"");
		sb.append(",\"acc_pwd\":\"" + "" + "\"");
		sb.append(",\"valid_date\":\"" + "" + "\"");// 卡有效期
		sb.append(",\"valid_no\":\"" + "" + "\"");// 卡安全码
		sb.append(",\"pay_code\":\"" + pay_code + "\"");// 银行编码
		sb.append(",\"sms_code\":\"" + sms_code + "\"");// =========短信验证码==========
		sb.append(",\"trade_date\":\"" + trade_date + "\"");
		sb.append(",\"additional_info\":\"" + additionalInfo + "\"");
		sb.append(",\"req_reserved\":\"" + reqReserved + "\"");
		sb.append("}");
	
		return sb.toString();
	}
		

	

	

	
}
